<?php	namespace Home\Controller;
/**
 * 评论管理控制器
 * @author 武斌   <wubin.mail@foxmail.com>
 */
class ContentController extends CommonController
{
	protected $db;
	/**
	 * 构造方法
	 */
	public function __init()
	{	//实例化商品模型
		$this->db = new \Home\Model\Goods;
	}
	/**
	 * 首页以及首页数据
	 */
	public function index()
	{
		//商品id
		$gid = Q('get.gid',0,'intval');
		//登陆
		if(IS_POST)
		{	//点击立即购买，没有登陆弹出登陆模态框
			$this->login($gid);
		}		
		//将商品id存入到session当中
		$_SESSION['gid'] = $gid;
		//获取商品表中单条数据,并分配变量
		$oneGoodsData = $this->db->where('gid',$gid)->first();
		View::with('goods',$oneGoodsData);
		//注入当前网页头部变量
		$pageConf = array(
			'css'=>'content',
			'js'=>'content|list',
			'title'=>$oneGoodsData['gname'],
		); 
		//分配当前网页公共头部变量 
		View::with('pageConf',$pageConf);
		//2.面包屑处理
		$data = Db::table('category')->get();
		//获取当前商品所属分类的父级
		$fatherData = $this->getFather($data, $oneGoodsData['category_cid']);
		//数组反转，将父级放到数组最前面
		$fatherData = array_reverse($fatherData);
		//分配变量 
		View::with('fatherData',$fatherData);
		//商品详情表图片处理
		$goodsDetail = Db::table('goodsdetail')->where('goods_gid',$gid)->first();
			$goodsDetail['small'] = explode(',', $goodsDetail['small']);
			$goodsDetail['medium'] = explode(',', $goodsDetail['medium']);
			$goodsDetail['big'] = explode(',', $goodsDetail['big']);
		View::with('goodsDetail',$goodsDetail);		
		//商品所属品牌
		$brand = Db::table('brand')->where("bid",$oneGoodsData['brand_bid'])->first();
		View::with('brand',$brand);
		//规格处理，商品表与类型属性表关联，得到该商品有哪些规格
		$goodsTypeAtte = Db::table('goods g')->join('typeattr ta','g.type_ID','=','ta.type_ID')->where('gid',$gid)->where('taclass',1)->get();
		//得到该商品在添加的时候添加了那些规格
		foreach ($goodsTypeAtte as $k => $v) {
			$goodsTypeAtte[$k]['spec'] = Db::table('goodsattr')->where('goods_gid',$gid)->where('typeAttr_taid',$v['taid'])->get();
		}
		View::with('goodsTypeAtte',$goodsTypeAtte);
		//属性处理，商品表与类型属性表关联，得到该商品有哪些属性
		$attr = Db::table('goods g')->join('typeattr ta','g.type_ID','=','ta.type_ID')->where('gid',$gid)->where('taclass',0)->get();
		//得到该商品在添加的时候添加了那些属性
		foreach ($attr as $k => $v) {
			$attr[$k]['attr'] = Db::table('goodsattr')->where('goods_gid',$gid)->where('typeAttr_taid',$v['taid'])->pluck('gavalue');
		}
		View::with('attr',$attr);
		//浏览过本商品的用户还浏览了
		$browse = $this->db->where("category_cid",$oneGoodsData['category_cid'])->orderBy('rand()')->limit(5)->get();
		View::with('browse',$browse);		
		//购买该商品的用户还购买了
		$hasBuy = Db::table('goods')->orderBy('rand()')->limit(4)->get();
		View::with('hasBuy',$hasBuy);
		//评论处理
		$commentData = Db::table('comment')->where("goods_gid",$gid)->orderBy('time','DESC')->get();
		if(count($commentData)!=0){
			foreach ($commentData as $k => $v)
			{
				$orderListData=Db::table('orderlist')->where("goods_gid",$gid)->first();
				if($orderListData)
				{
					$orderLData = Db::table('orderl')->where("oid",$orderListData['orderL_oid'])->first();
					$commentData[$k]['buyTime'] =$orderLData['time'];
					$commentData[$k]['size'] =$orderListData['glsize'];
					$commentData[$k]['color'] =$orderListData['glcolor'];
					$commentData[$k]['username'] =Db::table('user')->where('uid',$orderLData['user_uid'])->pluck('username');
				}
			}
			$count = count($commentData);//全部评价统计
			View::with('count',$count);
			View::with('commentData',$commentData);
		}
		//载入模板
		View::make(TEMPLATE . '/content.html');		
	}
	/**
	 * 异步处理规格
	 */
	public function ajaxSpec()
	{
		if(!IS_AJAX) return false;
		//post提交的用户选择的规格组合id，字符串的形式：（19，12，），
		$goodsListCombine = rtrim(Q('post.goodsListCombine',0,'intaval'),',');
		//在货品数据中查询该组合数据
		$goodList = Db::table('goodslist')->where("combine",$goodsListCombine)->first();
		//库存
		$glnumber = $goodList['glnumber'];
		//将组合规格转换为数组
		$goodListCombine = explode(',', $goodList['combine']);
		//在商品属性表中得到对应数据
			foreach ($goodListCombine as $k => $v) {
				$goodAttr[] = Db::table('goodsattr')->where("gaid",$v)->get();
			}
		//根据对应id得到对应的值		
			foreach ($goodAttr as $k => $v) {
				foreach ($v as $kk => $vv) {
					$goodAttrSpec['spec'][] = $vv['gavalue'];
				}
			}
		//将对应值转换为字符转
		$goodAttrSpec['spec']=implode(',', $goodAttrSpec['spec']);	
		$goodAttrSpec['glnumber'] = $glnumber;
		//将选择规格和库存返出
		View::ajax($goodAttrSpec);
	}
	/**
	 * 登陆
	 */
	public function login($gid)
	{
		//实例化User模型
		$userModel = new \Home\Model\User;
		//执行user模型里边的login方法
		if(!$userInfo = $userModel->login()){
			//提示错误信息
			$this->error($userModel->getError());
		}
		//账号锁定时相应提示信息	
		if($userInfo['is_lock'] == 1){
			$this->error('您的账号已被锁定,请与管理员联系');
		}	
		$_SESSION['uid']=$userInfo['uid'];
		$_SESSION['username']=$userInfo['username'];
		//登陆成功提示信息，跳转到商品内容页
		View::success('登陆成功',U('Content/index',array('gid'=>$gid)));				
	}
	/**
	 * 点击评论按钮的时候异步检测用户是否已购买过该商品
	 */
	public function ajaxIsBuy()
	{
		$gid = $_SESSION['gid'];//用户id
		$uid = $_SESSION['uid'];//商品id
		//查询订单表中该用户有无订单
		$orderL = Db::table('orderl')->where("user_uid",$uid)->get();
		//查询订单列表中该用户有无该商品订单记录
		foreach ($orderL as $k => $v) {
			$orderList =	Db::table('orderlist')->where("orderL_oid",$v['oid'])->where('goods_gid',$gid)->first();
		}
		//有记录
		if($orderList){
			View::ajax(1);
		}else{
			View::ajax(0);
		}				
	}
	/**
	 * 添加商品评论
	 */
	public function addPingLun()
	{
		if(IS_POST)
		{
			$gid = $_SESSION['gid'];
			$commentModel = new \Home\Model\Comment;
			//调用模型里边的添加进行操作
			if(!$commentModel->addData()){
				$this->error($commentModel->getError());
			}
			go(U('index',array('gid'=>$gid)));			
		}
	}
}
